package com.buba.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import entity.device.DeviceType;
import com.buba.mapper.DeviceTypeMapper;
import com.buba.service.DeviceTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import entity.sparemanage.SpareTypes;
import org.springframework.beans.factory.annotation.Autowired;
import entity.maintenance.MaintenanceLevel;
import entity.sparemanage.SpareTypes;
import entity.system.CodeRules;
import feign.systemFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author beisheng
 * @since 2023-05-18
 */
@Service
public class DeviceTypeServiceImpl extends ServiceImpl<DeviceTypeMapper, DeviceType> implements DeviceTypeService {
    @Autowired
    private DeviceTypeMapper deviceTypeMapper;

    @Autowired
    private feign.systemFeign systemFeign;

    /**
     * @return common.R
     * @Author atlong
     * @Description 添加设备类型信息
     * @Date 2023/6/1 9:35
     * @Param [deviceType]
     **/
    @Override
    public boolean insDeviceType(DeviceType deviceType) {
        //根据规则生成编号
        CodeRules byxm = systemFeign.selAllRules("SB");
        //查询时间格式
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(byxm.getDateTime());
        //拼接所需时间字符串
        String format = simpleDateFormat.format(new Date());
        //当前表长度
        int size = deviceTypeMapper.selectList(new QueryWrapper<DeviceType>().eq("delete_flag", 0)).size();
        //生成编码
        String code = byxm.getPrefix() + format + "0" + (size + 1);
        deviceType.setTypeCoding(code);
        deviceType.setCreateTime(new Date());
        deviceType.setDeleteFlag(0);
        int insert = deviceTypeMapper.insert(deviceType);
        return insert > 0;
    }

    /**
     * @return boolean
     * @Author atlong
     * @Description 批量删除
     * @Date 2023/6/1 9:44
     * @Param [idArray]
     **/
    @Override
    public boolean deleteAll(List<Integer> idArray) {
        ArrayList<Integer> ids = new ArrayList<>();
        for (Integer id : idArray) {
            List<DeviceType> list = deviceTypeMapper.selectList(new QueryWrapper<DeviceType>().eq("parent_id", id));
            if (list.size() == 0) {
                ids.add(id);
            } else {
                //先把要删除的一级分类id放入到集合中
                ids.add(id);
                //递归的将一级分类下的id也加入到集合中
                this.getIds(ids, id);
            }

        }
        int i = deviceTypeMapper.deleteBatchIds(ids);


        return i > 0;
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 树形查询设备类型
     * @Date: 2023/5/22 14:36
     * @Return: common.R
     * @Param: []
     */

    @Override
    public Object selAllDeviceType() {
        List<DeviceType> deviceTypeList = deviceTypeMapper.selectList(new QueryWrapper<DeviceType>().eq("delete_flag", 0));
        List<DeviceType> collect = deviceTypeList.stream().filter(i ->
                i.getParentId() == 0
        ).map(menu -> {
            menu.setChildren(getChidrens(menu,deviceTypeList));
            return menu;
        }).collect(Collectors.toList());
        return collect;
    }

    public List<DeviceType> getChidrens(DeviceType root,List<DeviceType> all){
        List<DeviceType> children  = all.stream().filter(carType -> {
            return carType.getParentId().equals(root.getId());
        }).map(carType -> {
            carType.setChildren(getChidrens(carType,all));
            return carType;
        }).collect(Collectors.toList());
        return children;
    }
    /**
     * @return common.R
     * @Author atlong
     * @Description 编辑设备类型信息
     * @Date 2023/6/1 14:29
     * @Param [deviceType]
     **/
    @Override
    public boolean updateDeviceType(DeviceType deviceType) {
        deviceType.setUpdateTime(new Date());
        UpdateWrapper<DeviceType> UpdateWrapper = new UpdateWrapper<>();
        UpdateWrapper.eq("id", deviceType.getId());
        UpdateWrapper.eq("delete_flag", 0);
        int update = deviceTypeMapper.update(deviceType, UpdateWrapper);
        return update > 0;

    }

    /**
     * @return boolean
     * @Author atlong
     * @Description 批量启动
     * @Date 2023/6/1 14:41
     * @Param [idArray]
     **/
    @Override
    public boolean startUsing(List<Integer> idArray) {
        ArrayList<Integer> ids = new ArrayList<>();
        for (Integer id : idArray) {
            List<DeviceType> list = deviceTypeMapper.selectList(new QueryWrapper<DeviceType>().eq("parent_id", id));
            if (list.size() == 0) {
                ids.add(id);
            } else {
                //先把要删除的一级分类id放入到集合中
                ids.add(id);
                //递归的将一级分类下的id也加入到集合中
                this.getIds(ids, id);
            }
        }
        UpdateWrapper<DeviceType> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id", ids);
        updateWrapper.set("state", 1);
        int update = deviceTypeMapper.update(null, updateWrapper);
        return update > 0;
    }

    /**
     * @return boolean
     * @Author atlong
     * @Description 批量停止
     * @Date 2023/6/1 14:42
     * @Param [idArray]
     **/
    @Override
    public boolean outService(List<Integer> idArray) {
        ArrayList<Integer> ids = new ArrayList<>();
        for (Integer id : idArray) {
            List<DeviceType> list = deviceTypeMapper.selectList(new QueryWrapper<DeviceType>().eq("parent_id", id));
            if (list.size() == 0) {
                ids.add(id);
            } else {
                //先把要删除的一级分类id放入到集合中
                ids.add(id);
                //递归的将一级分类下的id也加入到集合中
                this.getIds(ids, id);
            }
        }
        UpdateWrapper<DeviceType> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id", ids);
        updateWrapper.set("state", 2);
        int update = deviceTypeMapper.update(null, updateWrapper);
        return update > 0;
    }

    /**
     * @return common.R
     * @Author atlong
     * @Description 树形查询可以使用的类型
     * @Date 2023/6/1 17:06
     * @Param []
     **/
    @Override
    public List<DeviceType> queryType() {
        QueryWrapper<DeviceType> wrapper = new QueryWrapper<>();
        wrapper.eq("delete_flag", 0);
        wrapper.eq("state", 1);
        List<DeviceType> deviceTypeList = deviceTypeMapper.selectList(wrapper);
        List<DeviceType> collect = deviceTypeList.stream().filter(i ->
                Integer.valueOf(i.getParentId()) == 0
        ).map(menu -> {
            menu.setChildren(getChidrens(menu, deviceTypeList));
            return menu;
        }).collect(Collectors.toList());
        return collect;
    }

    /***
     * @Author atlong
     * @Description 查询设备类型信息
     * @Date 2023/6/14 8:53
     * @Param []
     * @return common.R
     **/
    @Override
    public List<DeviceType> selDeviceTypes() {
        QueryWrapper<DeviceType> wrapper = new QueryWrapper<>();
        wrapper.eq("delete_flag", 0);
        wrapper.eq("state", 1);
        List<DeviceType> deviceTypeList = deviceTypeMapper.selectList(wrapper);
        return deviceTypeList;
    }


    //树形删除修改递归方法
    private void getIds(ArrayList<Integer> ids, Integer oneId) {
        //查询二级分类的对象
        QueryWrapper<DeviceType> Wrapper = new QueryWrapper<>();
        Wrapper.eq("parent_id", oneId);
        List<DeviceType> list = deviceTypeMapper.selectList(Wrapper);

        //遍历二级分类的对象，把二级分类的id加入到要删除的集合中
        for (DeviceType deviceType : list) {
            Integer id = deviceType.getId();
            ids.add(id);
            //把二级分类的每一个ID，查询它下面的子节点
            this.getIds(ids, id);
        }
    }


    /**
     * @return java.util.List<entity.device.DeviceType>
     * @Author atlong
     * @Description 树形结构设备类型
     * @Date 2023/6/1 8:37
     * @Param [deviceType]
     **/
    @Override
    public List<DeviceType> selDeviceType(DeviceType deviceType) {

        QueryWrapper<DeviceType> wrapper = new QueryWrapper<>();
        wrapper.eq("delete_flag", 0);
        if (!deviceType.getTypeName().equals("")) {
            wrapper.like("Type_name", deviceType.getTypeName());
        }
        List<DeviceType> deviceTypeList = deviceTypeMapper.selectList(wrapper);
        List<DeviceType> collect = deviceTypeList.stream().filter(i ->
                Integer.valueOf(i.getParentId()) == 0
        ).map(menu -> {
            menu.setChildren(getChidrens(menu, deviceTypeList));
            return menu;
        }).collect(Collectors.toList());
        return collect;
    }


}
